package com.atguigu.tingshu;

import org.junit.jupiter.api.Test;
import org.redisson.api.RLock;
import org.redisson.api.RedissonClient;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;

import java.util.concurrent.TimeUnit;

/**
 * ClassName: RedissonLock
 * Package: com.atguigu.tingshu
 * Description:
 *
 * @Author 咚咚小圆帽
 * @Create 2025/7/28 Monday 19:50
 * @Version 1.0
 */
@SpringBootTest
public class RedissonLock {

    @Autowired
    private RedissonClient redissonClient;

    @Test
    public void testLock() {
        //1.创建锁对象
        RLock myLock = redissonClient.getLock("myLock");
        //2.获取分布式锁
        myLock.lock();
        //3.加锁成功后执行业务逻辑
        try {
            System.out.println("获取锁成功执行业务逻辑");
            TimeUnit.SECONDS.sleep(100);
        } catch (InterruptedException e) {
            throw new RuntimeException(e);
        } finally {
            //4.释放锁
            myLock.unlock();
        }


    }

}
